package com.rongke.web.apix;


import com.alipay.api.AlipayApiException;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.ZhimaCustomerCertificationCertifyRequest;
import com.alipay.api.request.ZhimaCustomerCertificationInitializeRequest;
import com.alipay.api.request.ZhimaCustomerCertificationQueryRequest;
import com.alipay.api.response.ZhimaCustomerCertificationCertifyResponse;
import com.alipay.api.response.ZhimaCustomerCertificationInitializeResponse;
import com.alipay.api.response.ZhimaCustomerCertificationQueryResponse;
import com.rongke.utils.OrderUtils;







/**
 *
 * @芝麻认证
 */

public class ZhimaAuth {
    //芝麻开放平台地址";
    private static String url  = "https://openapi.alipay.com/gateway.do";


    private static String appId = "2017061507500020";


    private static String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDH4zjyddtXqvPvEI4Bbnb9IRM3JwqXFRzqS2ZBgAYtalrppef1f5Jk5PdkCpDjMSF0xJCHpEMblxR9a/Q8ke4ecHvOl1v7vh4B1zCN5icr4iM22gcEvlbGS0/4dj/DBIZGmp9HEF3LpULmuhTRK+w+tjYy2mBYl+nLSozMi05DG/xt/qfkaFTuBbsRlvEtWK2uVNUrhsxSJx/kjsLKSToBBlY2GYfQvH3esBoIilpZ5VO165a78aI7zQQs0S2qKxDsSaYl8Aj+w/rKx2RXfVvQY27ZBsXZ/PyEGhwBb20WnE903cGPfSZEOQMa0kn02n1zWVefQLn3QFF0RhVwWs7PAgMBAAECggEAW0JJQKFAWEErh40WWF73ok/UdwyzEBFKl0l/UQ5DQ77X7O6a/YGV88x5ikosph1LOgV4OwVqwhDzAOQlqPWzMmCGleaSz1eYWZzGhTJ71dQz4ZofNDI2aef+heBu8fmB2IBFketN92jnJ3HziSA1htgn3IXJEQmiavcJW2WKgKZaLKoPgmNOgtZgT+EJ1oYeMetCcqvy/0/JMUEZtiRyN+Nj7dZb54XJVcJVJtgVxSvQGqsxlupuFib9EZtLLXOg10nh82D32jvTdsarVi6pbT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HLHAd5l0YeqbFzjA6Rs9mvY+giXv3KSiMH04ZfZpuvcNHqdr8eZu3NBApAjgAQXvv6aQsq67Ej+2dUcDNyU2MTEXbHTNpj+AaXyOWuijcdwlItxz9F4E0UpovtCEnfy3D204wzaSULxt/kBO2nDTFGjN8LkBmVRkbjiLymclP5e4+J3pyKWXcCFyLHHxtIPculiF8QFtAkRfDvMx7ze7ecIENVhjA1cyiUfkKE0c6av8kELN1AFgpzqzqm7VNeoWHt8np7J3L4CbyzpsQr5dRlXkCFwIDAQAB";



    public static String  zhimaCustomerCertificationInitialize(String userName,String idCard) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(url, appId, privateKey, "json", "GBK", alipayPublicKey, "RSA2");
        ZhimaCustomerCertificationInitializeRequest request = new ZhimaCustomerCertificationInitializeRequest();

        String transaction_id = "XQKD"+ OrderUtils.getOrderNo();
        request.setBizContent("{" +
                "\"transaction_id\":\""+transaction_id +"\"," +
                "\"product_code\":\"w1010100000000002978\"," +
                "\"biz_code\":\"FACE\"," +
                "\"identity_param\":\"{\\\"identity_type\\\":\\\"CERT_INFO\\\",\\\"cert_type\\\":\\\"IDENTITY_CARD\\\",\\\"cert_name\\\":\\\""+userName+"\\\",\\\"cert_no\\\":\\\""+idCard+"\\\"}\"," +
                "\"ext_biz_param\":\"{}\"" +
                "  }");
        ZhimaCustomerCertificationInitializeResponse response = alipayClient.execute(request);
        return  response.getBizNo();
    }


    public static String  zhimaCustomerCertificationCertify(String biz_no,String callbackUrl) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(url, appId, privateKey, "json", "GBK", alipayPublicKey, "RSA2");
        ZhimaCustomerCertificationCertifyRequest request = new ZhimaCustomerCertificationCertifyRequest();
        request.setBizContent("{" +
                "\"biz_no\":\""+biz_no+"\"" +
                "  }");

        request.setReturnUrl(callbackUrl);

        // ����һ��Ҫʹ��GETģʽ
        ZhimaCustomerCertificationCertifyResponse response = alipayClient.pageExecute(request, "GET");
        // ��body�л�ȡURL
        String url = response.getBody();
        return  url;
    }

    //��ѯ�Ƿ���֤�ӿ�
    public static String zhimaCustomerCertificationQuery(String biz_no) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(url, appId, privateKey, "json", "GBK", alipayPublicKey, "RSA2");
        ZhimaCustomerCertificationQueryRequest request = new ZhimaCustomerCertificationQueryRequest();
        request.setBizContent("{" +
                "\"biz_no\":\""+biz_no+"\"" +
                "  }");
        ZhimaCustomerCertificationQueryResponse response = alipayClient.execute(request);
        if (response.getCode().equals("10000")&&response.getPassed().equals("true")){
            return response.getCode();
        }else{
            return  response.getSubMsg();
        }
    }

    public static void main(String[] args) throws AlipayApiException {
        //String a=testZhimaCustomerCertificationInitialize();
        //String a = zhimaCustomerCertificationCertify();
        //System.out.println(a+"888888888888888888");
    }

}





